﻿
CREATE PROCEDURE [dbo].[proc_Finance_RunningAccount_Create]
	(
		@CreateDate datetime,
		@TradeDate varchar(20),
		@Summary nvarchar(50),
		@AccountsTypeId int,
		@AccountsTypeName nvarchar(10),
		@ParentId int,
		@ParentName nvarchar(50),
		@SubjectId int,
		@SubjectName nvarchar(50),
		@PaymentTypeId int,
		@PaymentTypeName nvarchar(50),
		@BankId int,
		@BankName nvarchar(50),
		@Money numeric(18,2),
		@Agent nvarchar(50),
		@AgentName nvarchar(50),
		@UserName nvarchar(50),
		@EName nvarchar(50),
		@Description nvarchar(200),
		@BillNumber nvarchar(50),
		@CompanyId int
	)
AS
begin
	Declare @Renling int
	Declare @Income numeric(18,2),@Outlay numeric(18,2)
	
	--赋值经手人姓名
	Select @AgentName=EName From Employee Where UserName=@Agent
	--赋值单位编号，创建人员姓名
	
	--Select @CompanyId=CompanyId, @EName=[Name] From aspnet_users Where UserName=@UserName
	
	--判断录入支出，赋值收入金额、支出金额、认领状态
	if(@AccountsTypeId=1)
		begin
			Set @Income=@Money
			Set @Outlay=0
			Set @Renling=1
		end
	else
		begin
			Set @Income=0
			Set @Outlay=@Money
			Set @Renling=2
		end
	--Insert
	--By GSJ 2012-08-14 将16:30后的数据调整到第2天 Begin--
	Declare @nDays int,@nHour int,@nMinute int,@dToday DateTime
	--Set @dToday = @CreateDate --初始化日期
	----16:30分调整日期
	--Set @nHour = 16
	--Set @nMinute = 30

	----今天+1天与下月1日进行比较，相同则说明今天是本月最后1天
	--If Month(@CreateDate)<12
	--	Set @nDays=DateDiff(d,DateAdd(d,1,@CreateDate),Convert(datetime,Convert(varchar(4),Year(@CreateDate)) + '-' + Convert(varchar(2),Month(@CreateDate)+1) + '-1'))
	--Else
	--	Set @nDays=DateDiff(d,DateAdd(d,1,@CreateDate),Convert(datetime,Convert(varchar(4),Year(@CreateDate)+1) + '-01-01'))
	--If @nDays>0 --不是最后1天
	--Begin
	--	If DatePart(hh,@CreateDate)>@nHour Or (DatePart(hh,@CreateDate)=@nHour And DatePart(mi,@CreateDate)>@nMinute)	--超过16:30分
	--		Set @dToday = DateAdd(hh,12,@CreateDate)	--加12小时
	--END
	Set @dToday=dbo.Get_SetDateTime()
	set @TradeDate =CONVERT(VARCHAR(10),@dToday,120)
--By GSJ 2012-08-14 将16:30后的数据调整到第2天 End --
	
	Insert Into Finance_RunningAccount
	(
		CreateDate,
		TradeDate,
		Summary,
		AccountsTypeId,
		AccountsTypeName,
		ParentId,
		ParentName,
		SubjectId,
		SubjectName,
		PaymentTypeId,
		PaymentTypeName,
		BankId,
		BankName,
		Income,
		Outlay,
		Agent,
		AgentName,
		UserName,
		EName,
		CompanyId,
		Description,
		BillNumber,
		Renling
	)
	Values
	(
		@dToday,
		@TradeDate,
		@Summary,
		@AccountsTypeId,
		@AccountsTypeName,
		@ParentId,
		@ParentName,
		@SubjectId,
		@SubjectName,
		@PaymentTypeId,
		@PaymentTypeName,
		@BankId,
		@BankName,
		@Income,
		@Outlay,
		@Agent,
		@AgentName,
		@UserName,
		@EName,
		@CompanyId,
		@Description,
		@BillNumber,
		@Renling
	)
	
	if(@AccountsTypeId=1)
	begin
		Declare @RId int
		Select @RId=@@IDENTITY
		
		Insert Into Finance_RunningAccountCopy
		(
			RId,
			CreateDate,
			TradeDate,
			Summary,
			AccountsTypeId,
			AccountsTypeName,
			PaymentTypeId,
			PaymentTypeName,
			BankId,
			BankName,
			Income,
			Outlay,
			UserName,
			EName,
			CompanyId,
			Description,
			BillNumber
		)
		Values
		(
			@RId,
			 @dToday,
			@TradeDate,
			@Summary,
			@AccountsTypeId,
			@AccountsTypeName,
			@PaymentTypeId,
			@PaymentTypeName,
			@BankId,
			@BankName,
			@Income,
			@Outlay,
			@UserName,
			@EName,
			@CompanyId,
			@Description,
			@BillNumber
		)
	end
end
	RETURN